<template>
  <div class="docker">
    <div
      v-for="(item, index) in dockerlist"
      :key="index"
      :class="{ docker__item_active: index === currentIndex }"
      class="docker__item"
      @click="handleDockerClick(index)"
    >
      <svg-icon class="iconfont" :name="index === currentIndex ? item.activeIcon : item.icon"></svg-icon>
      <div class="docker__title">{{ item.text }}</div>
    </div>
  </div>
</template>

<script lang="ts">
import { ref } from 'vue'
import { useRouter, RouteRecordName } from 'vue-router'

const switchFilter = (value: RouteRecordName | null | undefined): number => {
  switch (value) {
    case 'Delivery':
      return 0
    case 'Waybill':
      return 1
    case 'Mine':
      return 2
    default:
      return 0
  }
}
export default {
  name: 'Docker',

  setup() {
    const router = useRouter()
    const currentRoute = router.currentRoute.value
    const currentIndex = ref<number>(0)
    currentIndex.value = switchFilter(currentRoute.name)
    router.afterEach((to, from, next) => {
      currentIndex.value = switchFilter(to.name)
    })

    const dockerlist = [
      { icon: 'delivery', activeIcon: 'delivery-active', text: '发货' },
      { icon: 'waybill', activeIcon: 'waybill-active', text: '运单' },
      { icon: 'mine', activeIcon: 'mine-active', text: '我的' }
    ]

    function handleDockerClick(index: number) {
      if (index === 0) {
        router.push({ name: 'Delivery' })
      } else if (index === 1) {
        router.push({ name: 'Waybill' })
      } else if (index === 2) {
        router.push({ name: 'Mine' })
      }
    }
    return {
      dockerlist,
      currentIndex,
      handleDockerClick
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/style/variables.scss';

.docker {
  display: flex;
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ededed;
  color: $color-text-primary;
  background: #f9f9f9ff;
  box-sizing: border-box;
  &__item {
    flex: 1;
    text-align: center;
    .iconfont {
      margin: 6px 0 5px 0;
    }
  }
  &__item_active {
    .docker__title {
      color: $color-primary;
    }
  }

  &__title {
    font-size: 20px;
    // 浏览器默认展示的最小像素是12像素，要显示10像素，横向和纵向都缩50%
    transform: scale(0.5, 0.5);
    // 要设置缩放的中心点，这里是以中间和顶部为中心才是合理的位置
    transform-origin: center top;
    color: $color-text-tabba;
  }
}
</style>
